public class Solution328 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode oddEvenList(ListNode head) {
        if (head==null || head.next==null){
            return head;
        }
        ListNode odd=head,evenHead=head.next,evenTail=head.next;
        ListNode cur=head.next.next;
        while (cur!=null){
            if (cur.next==null){
                odd.next=cur;
                cur.next=evenHead;
                break;
            }else{
                ListNode n=cur.next;
                odd.next=cur;
                cur.next=evenHead;
                odd=cur;
                evenTail.next=n;
                evenTail=n;
                cur=n.next;
            }
        }
        evenTail.next=null;
        return head;
    }

    public static void main(String[] args) {
        System.out.println(new Solution328().oddEvenList(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5)))))).next.next.next.val);
    }
}
